<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\template;

interface ITrigger
{
  public function setScope($sScope);
  public function isCurrent();
  public function isClose();
  public function isNext();

  public function setInstruction(container\xhtml\instructions\IConcrete $oInstruction);
  public function executeCurrent();
  public function executeNext($sTag, $aAttributes);
  public function executeScan($sTag, $aAttributes);
  public function executeClose($sTag);

  public function getContent();
  public function getContentClose();
  public function getAttributes();
  public function getPostTag();

  public function rememberTag($sTag);

  public function incNestedCounter();
  public function decNestedCounter();
}

class Trigger implements ITrigger
{
  public function setScope($sScope)
  {
    $this->sScope = $sScope;
  }

  public function isCurrent()
  {
    $bReturn = (strpos($this->sScope, '_current') !== \FALSE);
    return $bReturn;
  }

  public function isNext()
  {
    $bReturn = (strpos($this->sScope, '_next') !== \FALSE);
    return $bReturn;
  }

  public function isClose()
  {
    $bReturn = (strpos($this->sScope, '_close') !== \FALSE);
    return $bReturn;
  }

  public function isScan()
  {
    $bReturn = (strpos($this->sScope, '_scan') !== \FALSE);
    return $bReturn;
  }

  public function setInstruction(container\xhtml\instructions\IConcrete $oInstruction)
  {
    $this->oInstruction = $oInstruction;
  }

  public function executeCurrent()
  {
    $this->oInstruction->executeCurrent();
  }

  public function executeNext($sTag, $aAttributes)
  {
    $this->oInstruction->executeNext($sTag, $aAttributes);
  }

  public function executeClose($sTag)
  {
    $this->oInstruction->executeClose($sTag);
  }

  public function executeScan($sTag, $aAttributes)
  {
    $this->oInstruction->executeScan($sTag, $aAttributes);
  }

  public function getContent()
  {
    return $this->oInstruction->getContent();
  }

  public function getContentClose()
  {
    return $this->oInstruction->getContentClose();
  }

  public function getAttributes()
  {
    return $this->oInstruction->getContentAttributes();
  }

  public function getContentInTag()
  {
    return $this->oInstruction->getContentInTag();
  }

  public function getPostTag()
  {
    return $this->oInstruction->getPostTag();
  }

  public function rememberTag($sTag)
  {
    $this->sTag = $sTag;
  }

  public function hasTag($sTag = '')
  {
    if ($sTag == '')
    {
      if (\is_null($this->sTag))
      {
        return \FALSE;
      }
      return \TRUE;
    }
    else
    {
      if ($this->sTag !== $sTag)
      {
        return \FALSE;
      }
      return \TRUE;
    }
  }

  public function incNestedCounter()
  {
    $this->iNestedCounter = $this->iNestedCounter + 1;
  }

  public function decNestedCounter()
  {
    $this->iNestedCounter = $this->iNestedCounter - 1;
  }

  public function getNestedCounter()
  {
    return $this->iNestedCounter;
  }

  /** PRIVATE *************************************************************************************/

  private $sScope = \NULL;
  private $oInstruction = \NULL;

  private $sTag = \NULL;
  private $iNestedCounter = 0;
}